package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.mapper.AdChannelMapper;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.vo.PageResultVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService{

    /**
     * 频道分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResultVo<AdChannel> pageList(ChannelPageRequestDto dto) {
        //构建分页对象
        Page<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
        //构建LambdaQueryWrapper对象
        LambdaQueryWrapper<AdChannel> lqw = new LambdaQueryWrapper<>();
        //如果状态不为空，则添加状态为查询条件
        lqw.eq(!StringUtils.isEmpty(dto.getStatus()),AdChannel::getStatus,dto.getStatus());
        //如果频道名不为空，则添加频道名为模糊查询对象
        lqw.like(!StringUtils.isEmpty(dto.getName()),AdChannel::getName,dto.getName());
        //调用mp的page方法查询
        page(page,lqw);
        //将分页查询的结果封装进返回对象里面
        PageResultVo<AdChannel> r = new PageResultVo<>();
        PageResultVo<AdChannel> result = PageResultVo.pageResult(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());
        return result;
    }
}
